#include "databasectl.h"
#include "serroutine.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <pthread.h>
#include <dirent.h>
#include "ddebug.h"
#include "transform.h"
#include "vacate.h"

extern pthread_mutex_t sqllock;
static int sendack(int socket, char type)
{
	char wbuf[4] = {0};
	wbuf[0] = 4;
	wbuf[1] = 0;
	wbuf[2] = 1;
	wbuf[3] = type;
	send(socket, wbuf, 4,0);
}

void routine(void *arg)
{
	unsigned char head[3]={0};
	char username[20]={0};
	char passwd[20]={0};
	unsigned char rbuf[1024] ={0};
	unsigned char wbuf[1024] = {0};
	int filelen;
	int ret;
	int recv_len;
	int send_len;
	int rett;
	int socket = (int)arg;
	//send from socket 3byte
	//
	


	while(1){
		sprintf(wbuf,"%s","Input Your name:\n");
		send_len = strlen(wbuf) + 1;
		send(socket, &send_len, sizeof(int), 0);
		send(socket, wbuf, send_len, 0);
		printf("send to client :%d\n", send_len);

		if(recv_from_client(socket, username))
			return;
/*		ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		ret = recv(socket,username,recv_len,MSG_WAITALL);
		printf("get name :%s\n",username);	
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
*/
		sprintf(wbuf,"%s","Input Your passwd:\n");
		send_len = strlen(wbuf) + 1;
		send(socket, &send_len, sizeof(int), 0);
		send(socket, wbuf, send_len, 0);
		printf("send to client :%d\n", send_len);
/*		ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		ret = recv(socket,passwd,recv_len,MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
*/
		if(recv_from_client(socket, passwd))
			return;

		printf("get passwd :%s\n",passwd);	
		if(0 != verify_user(username,passwd))
		{
			printf("verify ok :\n");	
			break;
		}
		sprintf(wbuf,"%s","Your passwd is error\n========================\nInput anykey to continue...\n");
		send_len = strlen(wbuf) + 1;
		send(socket, &send_len, sizeof(int), 0);
		send(socket, wbuf, send_len, 0);
/*
		ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		ret = recv(socket,rbuf,recv_len,MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		*/
	if(recv_from_client(socket,rbuf))
		return;
	}

	struct member user_src = {
		//.name = username,
		.fd = socket,
		.permision = get_permission(username),
	};
	strcpy(user_src.name, username);
	sprintf(wbuf,"%s","Your passwd is right\n======================\nInput anykey to continue...\n");
	send_len = strlen(wbuf) + 1;
	send(socket, &send_len, sizeof(int), 0);
	send(socket, wbuf, send_len, 0);
/*
	ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
	ret = recv(socket,rbuf,recv_len,MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
*/
	if(recv_from_client(socket,rbuf))
		return;

	while(1){
		Message_read(&user_src);
		sprintf(wbuf,"%s","Input Your choice \n0:vacate apply\n1:modify \n2:add_user \n3:del_user \n4:select \n5:message \n6:vacate check \n7:vacate contrl \nquit:exit\n");
		send_len = strlen(wbuf) + 1;
		send(socket, &send_len, sizeof(int), 0);
		send(socket, wbuf, send_len, 0);
/*
		ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		ret = recv(socket,rbuf,recv_len,MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
		*/
	if(recv_from_client(socket,rbuf))
		return;

		switch(rbuf[0])
		{
		case '0':
			Vacate_apply(&user_src);
			break;
		case '1':
			if(user_src.permision)
			{
				if((rett = change_manager(&user_src)) == 0)
						return;
			}
			else
			{
				if((rett = change_user(&user_src)) == 0)
					return;
			}	
			break;
		case '2':
			if(user_src.permision)
			{
				if((rett =manager_add_user(&user_src)) == 0)
					return;
			}
			else
			{
				sprintf(wbuf,"%s","Nothing to do \n======================\nInput anykey to continue...\n");
				send_len = strlen(wbuf) + 1;
				send(socket, &send_len, sizeof(int), 0);
				send(socket, wbuf, send_len, 0);
				if(recv_from_client(socket,rbuf))
					return;
			}
			break;
		case '3':
			if(user_src.permision)
			{
				if((rett = manager_del_user(&user_src)) == 0)
					return;
			}
			else
			{
				sprintf(wbuf,"%s","Nothing to do \n======================\nInput anykey to continue...\n");
				send_len = strlen(wbuf) + 1;
				send(socket, &send_len, sizeof(int), 0);
				send(socket, wbuf, send_len, 0);
				if(recv_from_client(socket,rbuf))
					return;
			}
			break;

		case '4':
			if((rett = my_select(username, socket)) == 0)
				return;
	//		ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
	//		ret = recv(socket,rbuf,recv_len,MSG_WAITALL);
			break;

		case '5':
			Message_write(&user_src);
			break;

		case 'Q':
			return;
		case '6':
			Vacate_check(&user_src);
			break;
		case '7':
			Vacate_contrl(&user_src);
			break;
		default:
			sprintf(wbuf,"%s","Nothing to do \n======================\nInput anykey to continue...\n");
			send_len = strlen(wbuf) + 1;
			send(socket, &send_len, sizeof(int), 0);
			send(socket, wbuf, send_len, 0);
			if(recv_from_client(socket,rbuf))
				return;

		/*			ret = recv(socket,&recv_len,sizeof(int),MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		}
			ret = recv(socket,rbuf,recv_len,MSG_WAITALL);
		if(ret <= 0)
		{
			printf("break pipo\n");
			return;
		} */

		}

/*
	sprintf(wbuf,"%s","======================\nInput anykey to continue...\n");
	send_len = strlen(wbuf) + 1;
	send(socket, &send_len, sizeof(int), 0);
	send(socket, wbuf, send_len, 0);
*/
}
}
